import React, { Component } from 'react'
import Qs from 'qs'
import axios from "../../../../http";
import moment from 'moment';
import {withRouter} from "react-router";
import {
    StyleSheet,
    Text,
    View
  } from 'react-native';
import {  Space,Toast} from 'antd-mobile'
import Calend, { CalendarView } from 'calend'
import 'calend/dist/styles/index.css';
import {OnNewEventClickData} from "calend";
import {OnEventClickData} from "calend";
import {OnPageChangeData} from "calend";
import {OnSelectViewData} from "calend";
import { EnvironmentOutline } from 'antd-mobile-icons';
import {PRIVATE_ROUTE} from "../../../../route.constants";
class MyReport extends Component {
  constructor(props) {

    super(props);

    this.state={eventList:[],userList:[],userViewList:[],modalVisible:false,modalEditVisible:false,modalViewVisible:false,calendarWeekends:true};

  }
  

  onNewEventClick = (data: OnNewEventClickData) => {
    // this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_SCHEDULE_ADD})
    console.log('onNewEventClick',data)
    let reportDate=JSON.stringify(data.day).substring(1,11)
    let start=JSON.stringify(data.day).substring(1,11)+" 9:00:00"
    let end=JSON.stringify(data.day).substring(1,11)+" 18:00:00"
    let weekDate=this.getWeekOfDate(reportDate);
    let existDayReport=this.checkExistReport(reportDate,"DAY")
    let state={}
    if(weekDate!='周五'){
      if(existDayReport==true){
          return ;
      }
      // this.setState({type:'DAY',workContentTitle:'当日',nextWorkPlanTitle:'次日',modalVisible:true,start:start,end:end,workContent:'',nextWorkPlan:'',memo:''})
      state={
        reportDate:reportDate,
        start:start,
        end:end,
        type:'DAY',
        workContentTitle:'当日',
        nextWorkPlanTitle:'次日',
        workContent:'',
        nextWorkPlan:'',
        memo:''
      }
      this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_REPORT_ADD},state=state)
      return ;
    }
  if(weekDate=='周五'){
    let existWeekReport=this.checkExistReport(reportDate,"WEEK")
    if(existDayReport==true&&existWeekReport==true){
        return ;
    }
    if(existDayReport==false){
      state={
        reportDate:reportDate,
        start:start,
        end:end,
        type:'DAY',
        workContentTitle:'当日',
        nextWorkPlanTitle:'次日',
        workContent:'',
        nextWorkPlan:'',
        memo:''
      }
      this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_REPORT_ADD},state=state)
        return ;
    }
    if(existWeekReport==false){
        state={
          reportDate:reportDate,
          start:start,
          end:end,
          type:'WEEK',
          workContentTitle:'本周',
          nextWorkPlanTitle:'下周',
          workContent:'',
          nextWorkPlan:'',
          memo:''
        }
        this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_REPORT_ADD},state=state)
        return ;
    }
  }
    
    

  };
  onEventClick = (data: OnEventClickData) => {

    console.log('onEventClick-data',data)
    let reportDate=this.format_state(data.startAt)
    let start=this.format_state(data.startAt)+" 9:00:00"
    let end=this.format_state(data.startAt)+" 18:00:00"
    let state
    // console.log(start)
    if(data.type=='WEEK'){
      state={
        id:data.id,
        reportDate:reportDate,
        start:start,
        end:end,
        type:'WEEK',
        workContentTitle:'本周',
        nextWorkPlanTitle:'下周',
        workContent:data.workContent,
        nextWorkPlan:data.nextWorkPlan,
        memo:data.memo
      }
      this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_REPORT_EDIT},state=state)
      return ;

    }
    if(data.type='Day'){
      state={
        id:data.id,
        reportDate:reportDate,
        start:start,
        end:end,
        type:'WEEK',
        workContentTitle:'当日',
        nextWorkPlanTitle:'次日',
        workContent:data.workContent,
        nextWorkPlan:data.nextWorkPlan,
        memo:data.memo
      }
      this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_REPORT_EDIT},state=state)
      return ;


    }


  }
  
  componentDidMount() {

  }
  getWeekOfDate=(dateString)=> {
    let dateArray = dateString.split("-");
    let date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
    return "周" + "日一二三四五六".charAt(date.getDay());
};
  showErrorMessage (systemMessage){
    Toast.show({
        content:  systemMessage,
        afterClose: () => {
            console.log('after')
          },
        
      })
  };
  checkExistReport = (reportDate, type) => {
    // console.log('reportdate',reportDate)
    // console.log('this.eventList',this.state.eventList)
    let eventApiList = this.state.eventList
    for(let key in eventApiList){
      if(this.format(key)===reportDate){
        for(let i in eventApiList[key]){
          if(eventApiList[key][i].type===type){
            return true ;
          }

        }
      }
    }
        
    
    return false;
    }
  format=(date)=>{
    // return console.log('date',date.substring(0,2),date.substring(3,5),date.substring(6,10));
    // console.log('date',date.substring(6,10)+date.substring(2,6)+date.substring(0,2))
    return date.substring(6,10)+date.substring(2,6)+date.substring(0,2)
  }
  format_state=(date)=>{
    return moment(date).format('YYYY-MM-DD');
}
  onPageChange = (data: OnPageChangeData) => {
    this.setState({"eventList":{}});
    axios.post('api/oa/work/report/findMyAll', Qs.stringify({
      start:moment(data.rangeFrom).format('YYYY-MM-DD HH:mm:ss'),
      end:moment(data.rangeTo).format('YYYY-MM-DD HH:mm:ss')
    }),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
        .then((response)=>{
          let  data=response.data.data;
          for(let i=0;i<data.length;i++){
              if(data[i]["type"]=='WEEK'){
                  data[i]["color"]='blue'
              }
          }


          let stateEventList=this.state.eventList;
          for(let i=0;i<data.length;i++){
            let event=data[i];
            let eventDate=moment(event.start).format('DD-MM-YYYY HH:mm:ss').substring(0,10);
            if (stateEventList[eventDate]==undefined){
              let newEventList= new Array()
              stateEventList[eventDate]=newEventList
          }
          let newEvent={
            id:event.id,
            startAt: moment(event.start).utc().format(),
            endAt: moment(event.end).utc().format(),
            summary: event.title,
            type:event.type,
            workContent:event.workContent,
            memo:event.memo,
            nextWorkPlan:event.nextWorkPlan,
            // color:(new Date(event.start))>(new Date())?'green':'gray',
            color:event.color?event.color:((new Date(event.start))>(new Date())?'green':'gray'),
          };
          stateEventList[eventDate].push(newEvent);
        }


        this.setState({eventList:stateEventList})
            })
            .catch(function (error) {
                this.showErrorMessage(error);

        });
  }
//   loadEvents=(eventInfo,successCallback, failureCallback)=>{
//     axios.post('api/oa/work/report/findMyAll', Qs.stringify({
//         start:moment(eventInfo.start).format('YYYY-MM-DD HH:mm:ss'),
//         end:moment(eventInfo.end).format('YYYY-MM-DD HH:mm:ss')
//     }),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
//         .then((response)=>{
//             let  data=response.data.data;
//             for(let i=0;i<data.length;i++){
//                 if(data[i]["type"]=='WEEK'){
//                     data[i]["backgroundColor"]='green'
//                 }
//             }
//             successCallback (data)
//         })
//         .catch(function (error) {
//             this.showErrorMessage(error);
//         });
// }


  render() {
    
    return (
      <Calend
      onEventClick={this.onEventClick}
      onNewEventClick={this.onNewEventClick}
      // onNewEventClick={onNewEventClick}
      events={this.state.eventList}
      initialDate={new Date().toISOString()}
      hourHeight={30}
      initialView={CalendarView.WEEK}
      disabledViews={[]}
     
      //  selectedView={selectedView}
      onPageChange={this.onPageChange}
    />
    );
  }
}
export default withRouter(MyReport)